package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import Models.CommonBrewSupplies;

public class CommonBrewSuppliesPresenter extends AbstractPresenter<CommonBrewSupplies> {
	final String SINGLEITEM = "commonBrewSupplies";
	final String MULTIITEM = "commonBrewSuppliess";
	
	public ArrayList<CommonBrewSupplies> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public CommonBrewSupplies tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public CommonBrewSupplies createItemFromJSONObject(JSONObject jsonObject){
		CommonBrewSupplies item = new CommonBrewSupplies();
		try{
			item.setAmount(jsonObject.getDouble("amount"));
			item.setBoilTime(jsonObject.getInt("boilTime"));
			item.setId(jsonObject.getInt("boilTime"));
			item.setUnits(jsonObject.getString("units"));
			
			POSSupplyViewPresenter presenter = new POSSupplyViewPresenter();
			JSONObject jsonGroup = jsonObject.getJSONObject("supplyID");
			item.setSupply(presenter.createItemFromJSONObject(jsonGroup));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(CommonBrewSupplies item){
		try{
			JSONObject json = new JSONObject();
			json.put("amount", item.getAmount());
			json.put("boilTime", item.getBoilTime());
			json.put("boilTime", item.getBoilTime());
			json.put("units", item.getUnits());
			
			POSSupplyViewPresenter presenter = new POSSupplyViewPresenter();
			json.put("supplyID", presenter.createJSON(item.getSupply()));
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public CommonBrewSupplies getEmptyEntity(){
		CommonBrewSupplies entity = new CommonBrewSupplies();
		return entity;
	}
}
